package com.example.computer_exam_training_sys.service.impl;

import com.example.computer_exam_training_sys.entity.UserWrongQuestionsInfo;
import com.example.computer_exam_training_sys.mapper.UserWrongQuestionsMapper;
import com.example.computer_exam_training_sys.service.UserWrongQuestionsService;
import com.example.computer_exam_training_sys.vo.PageResult;
import com.example.computer_exam_training_sys.vo.UserWrongQuestionWithCourseVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.core.JsonProcessingException;

import java.util.List;
import com.example.computer_exam_training_sys.vo.QuestionWithPoint;

@Service
public class UserWrongQuestionsServiceImpl implements UserWrongQuestionsService {

    @Autowired
    private UserWrongQuestionsMapper mapper;

    @Autowired
    private ObjectMapper objectMapper; // 注入 ObjectMapper

    @Override
    public List<UserWrongQuestionsInfo> findAll() {
        return mapper.findAll();
    }

    @Override
    public List<UserWrongQuestionsInfo> findByUserId(Integer userId) {
        return mapper.findByUserId(userId);
    }

    @Override
    public List<UserWrongQuestionsInfo> findByQuestionId(Integer questionId) {
        return mapper.findByQuestionId(questionId);
    }

    @Override
    public int insert(UserWrongQuestionsInfo info) {
        return mapper.insertOrUpdate(info);
    }

    @Override
    public int update(UserWrongQuestionsInfo info) {
        return mapper.update(info);
    }

    @Override
    public int softDelete(Integer id) {
        return mapper.softDelete(id);
    }

    @Override
    public int hardDelete(Integer id) {
        return mapper.hardDelete(id);
    }

    @Override
    public List<QuestionWithPoint> selectQuestionsWithPointByPointId(Integer pointId) {
        return mapper.selectQuestionsWithPointByPointId(pointId);
    }

    @Override
    public List<UserWrongQuestionWithCourseVO> findWrongQuestionsByCourseId(Integer courseId) {
        return mapper.findWrongQuestionsByCourseId(courseId);
    }

    @Override
    public PageResult<UserWrongQuestionWithCourseVO> queryWrongQuestions(
            Integer userId, Integer courseId, Integer pointId, Integer isMastered,
            Integer sourceType, String startTime, String endTime, Integer page, Integer size) {

        int offset = (page - 1) * size;
        List<UserWrongQuestionWithCourseVO> list = mapper.queryWrongQuestions(
                userId, courseId, pointId, isMastered, sourceType, startTime, endTime, offset, size
        );
        int total = mapper.countWrongQuestions(
                userId, courseId, pointId, isMastered, sourceType, startTime, endTime
        );
        return new PageResult<>(list, total, size, page);
    }

    @Override
    public List<UserWrongQuestionWithCourseVO> findWrongQuestionsByIds(List<Integer> ids) {
        List<UserWrongQuestionWithCourseVO> list = mapper.findWrongQuestionsByIds(ids);
        for (UserWrongQuestionWithCourseVO vo : list) {
            if (vo.getAnswer() != null) {
                try {
                    Answer answer = objectMapper.readValue(vo.getAnswer(), Answer.class);
                    vo.setCorrectAnswer(answer.getCorrect() != null ? answer.getCorrect() : "");
                } catch (JsonProcessingException e) {
                    vo.setCorrectAnswer("");
                }
            } else {
                vo.setCorrectAnswer("");
            }
        }
        return list;
    }

    // 内部类用于解析JSON
    private static class Answer {
        private String correct;

        public String getCorrect() {
            return correct;
        }

        public void setCorrect(String correct) {
            this.correct = correct;
        }
    }

    @Override
    public int updateMastered(Integer id, Byte isMastered) {
        UserWrongQuestionsInfo info = new UserWrongQuestionsInfo();
        info.setId(id);
        info.setIsMastered(isMastered);
        return mapper.updateMastered(info);
    }

    @Override
    public int countDistinctQuestionsByUserId(Integer userId) {
        return mapper.countDistinctQuestionsByUserId(userId);
    }
}